﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ElectricalTest
{

    #region Can类
    public struct VCI_BOARD_INFO
    {
        public UInt16 hw_Version;
        public UInt16 fw_Version;
        public UInt16 dr_Version;
        public UInt16 in_Version;
        public UInt16 irq_Num;
        public byte can_Num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public byte[] str_Serial_Num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
        public byte[] str_hw_Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] Reserved;
    }

    /////////////////////////////////////////////////////
    //2.定义CAN信息帧的数据类型。
    unsafe public struct VCI_CAN_OBJ  //使用不安全代码
    {
        public uint ID;
        public uint TimeStamp;        //时间标识
        public byte TimeFlag;         //是否使用时间标识
        public byte SendType;         //发送标志。保留，未用
        public byte RemoteFlag;       //是否是远程帧
        public byte ExternFlag;       //是否是扩展帧
        public byte DataLen;          //数据长度
        public fixed byte Data[8];    //数据
        public fixed byte Reserved[3];//保留位

    }

    //3.定义初始化CAN的数据类型
    public struct VCI_INIT_CONFIG
    {
        public UInt32 AccCode;
        public UInt32 AccMask;
        public UInt32 Reserved;
        public byte Filter;   //0或1接收所有帧。2标准帧滤波，3是扩展帧滤波。
        public byte Timing0;  //波特率参数，具体配置，请查看二次开发库函数说明书。
        public byte Timing1;
        public byte Mode;     //模式，0表示正常模式，1表示只听模式,2自测模式
    }

    /*------------其他数据结构描述---------------------------------*/
    //4.USB-CAN总线适配器板卡信息的数据类型1，该类型为VCI_FindUsbDevice函数的返回参数。
    public struct VCI_BOARD_INFO1
    {
        public UInt16 hw_Version;
        public UInt16 fw_Version;
        public UInt16 dr_Version;
        public UInt16 in_Version;
        public UInt16 irq_Num;
        public byte can_Num;
        public byte Reserved;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] str_Serial_Num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] str_hw_Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] str_Usb_Serial;
    }

    /*------------数据结构描述完成---------------------------------*/

    public struct CHGDESIPANDPORT
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public byte[] szpwd;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public byte[] szdesip;
        public Int32 desport;

        public void Init()
        {
            szpwd = new byte[10];
            szdesip = new byte[20];
        }
    }
    #endregion
    public partial class Form1 : Form
    {
        //public delegate void ReceiveDataDelegate(byte[] readBuffer);
        //public event ReceiveDataDelegate ReceiveDataEvent;//接收数据处理事件
        const int DEV_USBCAN = 3;
        const int DEV_USBCAN2 = 4;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DeviceType"></param>
        /// <param name="DeviceInd"></param>
        /// <param name="Reserved"></param>
        /// <returns></returns>
        /*------------兼容ZLG的函数描述---------------------------------*/
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_OpenDevice(UInt32 DeviceType, UInt32 DeviceInd, UInt32 Reserved);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_CloseDevice(UInt32 DeviceType, UInt32 DeviceInd);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig);

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_ReadBoardInfo(UInt32 DeviceType, UInt32 DeviceInd, ref VCI_BOARD_INFO pInfo);

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_GetReceiveNum(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_ClearBuffer(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_StartCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_ResetCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pReceive, UInt32 Len, Int32 WaitTime);

        /*------------其他函数描述---------------------------------*/

        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_ConnectDevice(UInt32 DevType, UInt32 DevIndex);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_UsbDeviceReset(UInt32 DevType, UInt32 DevIndex, UInt32 Reserved);
        [DllImport("controlcan.dll")]
        static extern UInt32 VCI_FindUsbDevice(ref VCI_BOARD_INFO1 pInfo);
        /*------------函数描述结束---------------------------------*/


        //比较上下限
        static double dianZuUp = 0;
        static double dianZuDown = 0;
        //static double dianLiuHighUp = 0;
        //static double dianLiuHighDown = 0;
        static double dianLiuMidUp = 0;
        static double dianLiuMidDown = 0;
        //static double dianLiuLowUp = 0;
        //static double dianLiuLowDown = 0;

        //public int AutoModel = 0;//0自动  1手动

        static UInt32 m_devtype = 4;//USBCAN2

        UInt32 m_bOpen = 0;
        UInt32 m_devind = 0;
        UInt32 m_canind = 0;

        VCI_CAN_OBJ[] m_recobj = new VCI_CAN_OBJ[1000];

        UInt32[] m_arrdevtype = new UInt32[20];

        public IniFile.IniFile _ini;
        //public Dictionary<string, CodeClass> codeDic = new Dictionary<string, CodeClass>();
        //List<CheckResultClass> checkResultList = new List<CheckResultClass>();

        public bool isStartCheck = true;
        private SerialPort dianliuPort;
        private SerialPort dianzuPort;
        private ScanerHook listener;
        //串口参数
        private string dianliuPortName;
        private string dianzuPortName;
      
        public EleCodeData ecd=new EleCodeData();

        public ResCodeData rcd=new ResCodeData();
        public string targetPath;
        private ushort[] registerBuffer;
        List<double> dianList = new List<double>();
        //public Thread thLeftCheck;
        //public Thread thRightCheck;
        string[] tempDianLiuMid;
        //XmlDocument doc = new XmlDocument();
        int scanControl = 0;//0：扫总码  1：扫左码  2：扫右码

        List<string> ruleList = new List<string>();
        //Model.Check_Product checkProModel = new Model.Check_Product();
        List<Model.Check_Product> checkProList = new List<Model.Check_Product>();
        BLL.Check_Product checkProBll = new BLL.Check_Product();

        List<Model.Code_Rule> codeRuleList = new List<Model.Code_Rule>();
        BLL.Code_Rule codeRuleBll = new BLL.Code_Rule();


        Model.Check_Result checkResModel = new Model.Check_Result();
        List<Model.Check_Result> checkResList = new List<Model.Check_Result>();
        BLL.Check_Result checkResBll = new BLL.Check_Result();
      
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            //DateTime dt = DateTime.Now;
            //cp= cpBll.GetModel("1111");
            //dgLingjian.EnableHeadersVisualStyles = false;
            //dgLingjian.AllowUserToAddRows = false;

            dgLingjian.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 10.5F);//, System.Drawing.FontStyle.Bold
            dgLingjian.DefaultCellStyle.Font = new Font("微软雅黑", 10.5F);//, System.Drawing.FontStyle.Bold
           


            dgCheckPara.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 10.5F);
            dgCheckPara.DefaultCellStyle.Font = new Font("微软雅黑", 10.5F);

            dgRule.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 10.5F);
            dgRule.DefaultCellStyle.Font = new Font("微软雅黑", 10.5F);


            dgResult.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 10.5F);
            dgResult.DefaultCellStyle.Font = new Font("微软雅黑", 10.5F);

            dgCheck.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 20F, System.Drawing.FontStyle.Bold);
            dgCheck.DefaultCellStyle.Font = new Font("微软雅黑", 20F, System.Drawing.FontStyle.Bold);

            picCheck.BackgroundImage = Properties.Resources.Check;
            //tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            button1.ForeColor = Color.Lime;
            tabControl1.Dock = DockStyle.Fill;
            panel3.Dock = DockStyle.Fill;

            panel2.Controls.Add(tabControl1);
            panel2.Controls.Add(panel3);

            toolStripButton1.Visible = false;
            toolStripButton2.Visible = false;
            tabControl1.Visible = false;
            panel3.Visible = true;

            //doc.Load(Application.StartupPath + @"\code.xml");
            //tabControl1.DrawItem += new DrawItemEventHandler(this.tabsControl_DrawItem);
            //dgAgvInfo.EnableHeadersVisualStyles = false;//这一句很重要，否则下面的列头设置不起作用
            ////dgTask.AllowUserToAddRows = false;
            //dgAgvInfo.AllowUserToAddRows = false;
            //dgAgvInfo.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold);
            //dgAgvInfo.DefaultCellStyle.Font = new Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold);
            _ini = new IniFile.IniFile(Application.StartupPath + @"\config.ini");

            string tempV = _ini.IniReadValue("VehicleMode", "value");

            if (tempV != "0" && tempV != "" && tempV != null)
            {
                tempDianLiuMid = tempV.Split('|');//string[] 
                for (int i = 0; i < tempDianLiuMid.Length; i++)
                {
                    ((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add(tempDianLiuMid[i]);
                }
            }


            //GetCodeData();
            GetCodeDataDataBase();
            GetCodeRuleDataBase();
            GetCheckData();
            GetSerialData();
            targetPath = Application.StartupPath + @"\CheckRecord.xlsx";
           
            //thLeftCheck = new Thread(new ThreadStart(LeftCheckThMethod));
            //thLeftCheck.IsBackground = true;

            //thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
            //thRightCheck.IsBackground = true;
            InitHook();

            //tabControl1.Dock = DockStyle.Fill;
            //panel2.Controls.Add(tabControl1);
            //OpenExcel();

            //checkResModel.part_id = "1212";
            //checkResModel.vehicle_name = "1212";
            //checkResModel.seat_loc = "1212";
            //checkResModel.left_check_starttime = DateTime.Now;
            //checkResModel.left_check_endtime = DateTime.Now;
            //checkResModel.right_check_starttime = DateTime.Now;
            //checkResModel.right_check_starttime = DateTime.Now;
            //checkResBll.Add(checkResModel);
        }

        #region 处理xml/ini
        //private void GetCodeData()
        //{
           
        //    XmlNodeList node = doc.GetElementsByTagName("appSettings");
        //    XmlNodeList nodeIp = node[0].ChildNodes;
        //    foreach (XmlNode xn in nodeIp)
        //    {
        //        XmlElement xe = (XmlElement)xn;

        //        string num = xe.GetAttribute("key").ToString();
        //        string[] codeStr = xe.GetAttribute("value").ToString().Split('|');


        //       if (codeStr.Length >= 8 && !codeDic.ContainsKey(num))
        //       {
        //           CodeClass cc = new CodeClass();
        //           cc.SeatName = codeStr[0];
        //           cc.SeatLoc = "后排";
        //           cc.PartNum = num;
        //           cc.LeftCode = codeStr[1];
        //           cc.RightCode = codeStr[2];
        //           cc.Sbr1Code = codeStr[3];
        //           cc.Sbr2Code = codeStr[4];
        //           cc.Sbr3Code = codeStr[5];
        //           cc.IsCheckDianLiu = Convert.ToBoolean(codeStr[6]);
        //           cc.IsCheckDianZu = Convert.ToBoolean(codeStr[7]);
        //           codeDic.Add(num, cc);
        //       }
        //    }



        //    if (codeDic.Count > 0)
        //    {
        //        foreach (string item in codeDic.Keys)
        //        {
        //            int index = dgLingjian.Rows.Add();
        //            dgLingjian.Rows[index].ReadOnly = true;
        //            dgLingjian.Rows[index].Cells[0].Value = codeDic[item].SeatName;
        //            dgLingjian.Rows[index].Cells[1].Value = "后排";
        //            dgLingjian.Rows[index].Cells[2].Value = codeDic[item].PartNum;
        //            dgLingjian.Rows[index].Cells[3].Value = codeDic[item].LeftCode;
        //            dgLingjian.Rows[index].Cells[4].Value = codeDic[item].RightCode;
        //            dgLingjian.Rows[index].Cells[5].Value = codeDic[item].Sbr1Code;
        //            dgLingjian.Rows[index].Cells[6].Value = codeDic[item].Sbr2Code;
        //            dgLingjian.Rows[index].Cells[7].Value = codeDic[item].Sbr3Code;
        //            dgLingjian.Rows[index].Cells[8].Value = codeDic[item].IsCheckDianLiu;
        //            dgLingjian.Rows[index].Cells[9].Value = codeDic[item].IsCheckDianZu;
        //        }

        //    }

       

        //}

        private void GetCodeDataDataBase()
        {
            checkProList = checkProBll.GetModelList("");

            foreach (Model.Check_Product item in checkProList)
            {
                int index = dgLingjian.Rows.Add();
                dgLingjian.Rows[index].ReadOnly = true;
                dgLingjian.Rows[index].Cells[0].Value = item.vehicle_type;
                dgLingjian.Rows[index].Cells[1].Value = item.seat_loc;
                dgLingjian.Rows[index].Cells[2].Value = item.part_id;
                dgLingjian.Rows[index].Cells[3].Value = item.left_code;
                dgLingjian.Rows[index].Cells[4].Value = item.right_code;
                dgLingjian.Rows[index].Cells[5].Value = item.sbr1_code;
                dgLingjian.Rows[index].Cells[6].Value = item.sbr2_code;
                dgLingjian.Rows[index].Cells[7].Value = item.sbr3_code;
                dgLingjian.Rows[index].Cells[8].Value = item.is_check_dianliu;
                dgLingjian.Rows[index].Cells[9].Value = item.is_check_dianzu;
            }

            dgLingjian.ClearSelection();


            //string st = "10#402003718AAABK$11#8MQ$12#202204150005$";
            //int s = st.Length;
            //string ss=st.Substring(0, 20);
         
        }


        private void GetCodeRuleDataBase()
        {
            codeRuleList = codeRuleBll.GetModelList("");

            ruleList.Clear();

            foreach (Model.Code_Rule item in codeRuleList)
            {
                int index = dgRule.Rows.Add();
                dgRule.Rows[index].ReadOnly = true;
                dgRule.Rows[index].Cells[0].Value = item.rule_name;
                dgRule.Rows[index].Cells[1].Value = item.rule_detail;
                dgRule.Rows[index].Cells[2].Value = item.rule_describe;


                ruleList.Add(item.rule_detail);
            }

            dgRule.ClearSelection();



            //for (int i = 0; i < ruleList.Count; i++)
            //{
            //    string tempLength = ruleList[i].Split(';')[0];
            //    string tempId = ruleList[i].Split(';')[1];

            //    int startAdd = int.Parse(tempId.Split('-')[0]);
            //    int subLength = int.Parse(tempId.Split('-')[1]);

            //    string st = "10#402003718AAABK$11#8MQ$12#202204150005$";
            

            //    string tempPartId = st.Substring(startAdd - 1, subLength - startAdd + 1);
            //}
        }


        //private void WriteCodeData(string partNum,string codeData)
        //{

        //    XmlNodeList node = doc.GetElementsByTagName("appSettings");
        //    XmlElement cust = doc.CreateElement("add");
        //    cust.SetAttribute("key", partNum);
        //    cust.SetAttribute("value", codeData);
        //    node[0].AppendChild(cust);
          
        //    doc.Save("code.xml");
        //}

        private void GetCheckData()
        {
            string dianZu = _ini.IniReadValue("CheckValue", "resistance");
            //string dianLiuHigh = _ini.IniReadValue("CheckValue", "currentHigh");
            string dianLiuMid = _ini.IniReadValue("CheckValue", "currentMid");
            //string dianLiuLow = _ini.IniReadValue("CheckValue", "currentLow");

            if (dianZu != "0" && dianZu != "" && dianZu != null)
            {
                string[] tempDianZu = dianZu.Split('-');
                dianZuDown = Convert.ToDouble(tempDianZu[0]);
                dianZuUp = Convert.ToDouble(tempDianZu[1]);
            }

            //if (dianLiuHigh != "0" && dianLiuHigh != "" && dianLiuHigh != null)
            //{
            //    string[] tempDianLiuHigh = dianLiuHigh.Split('-');
            //    dianLiuHighDown = Convert.ToDouble(tempDianLiuHigh[0]);
            //    dianLiuHighUp = Convert.ToDouble(tempDianLiuHigh[1]);
            //}

            if (dianLiuMid != "0" && dianLiuMid != "" && dianLiuMid != null)
            {
                string[] tempDianLiuMid = dianLiuMid.Split('-');
                dianLiuMidDown = Convert.ToDouble(tempDianLiuMid[0]);
                dianLiuMidUp = Convert.ToDouble(tempDianLiuMid[1]);
            }

            //if (dianLiuLow != "0" && dianLiuLow != "" && dianLiuLow != null)
            //{
            //    string[] tempDianLiuLow = dianLiuLow.Split('-');
            //    dianLiuLowDown = Convert.ToDouble(tempDianLiuLow[0]);
            //    dianLiuLowUp = Convert.ToDouble(tempDianLiuLow[1]);
            //}

            IniCheckComData();


          
          
        }
        private void WriteCheckData(string name,string value)
        {
            _ini.IniWriteValue("CheckValue", name, value);
        }

        private void IniCheckComData()
        {
            for (int i = 0; i < 2; i++)
            {
                int index = dgCheckPara.Rows.Add();
                if (i == 0)
                {
                    dgCheckPara.Rows[index].Cells[0].Value = "SBR（电阻）";
                    dgCheckPara.Rows[index].Cells[1].Value = dianZuDown;
                    dgCheckPara.Rows[index].Cells[2].Value = dianZuUp;
                }
                else if (i == 1)
                {
                    dgCheckPara.Rows[index].Cells[0].Value = "加热垫（电流）";//
                    dgCheckPara.Rows[index].Cells[1].Value = dianLiuMidDown;
                    dgCheckPara.Rows[index].Cells[2].Value = dianLiuMidUp;
                }
                //else if (i == 2)
                //{
                //    dgCheckPara.Rows[index].Cells[0].Value = "加热垫（电流）二档";
                //    dgCheckPara.Rows[index].Cells[1].Value = dianLiuMidDown;
                //    dgCheckPara.Rows[index].Cells[2].Value = dianLiuMidUp;
                //}
                //else if (i == 3)
                //{
                //    dgCheckPara.Rows[index].Cells[0].Value = "加热垫（电流）三档";
                //    dgCheckPara.Rows[index].Cells[1].Value = dianLiuLowDown;
                //    dgCheckPara.Rows[index].Cells[2].Value = dianLiuLowUp;
                //}


            }
        }


        private void GetSerialData()
        {
            dianliuPortName = _ini.IniReadValue("DianLiuCom", "value");
            dianzuPortName = _ini.IniReadValue("DianZuCom", "value");

            InitSerialPortParameter();
        }
        #endregion


        private void InitHook()
        {
            listener = new ScanerHook();
            listener.ScanerEvent += Listener_ScanerEvent;
            listener.Start();

        }

        private void InitSerialPortParameter()
        {
       
                try
                {
                    dianliuPort = new SerialPort(dianliuPortName, 9600, Parity.None, 8, StopBits.One);
                    dianliuPort.Open();

                    dianzuPort = new SerialPort(dianzuPortName, 9600, Parity.None, 8, StopBits.One);
                    dianzuPort.DataReceived += new SerialDataReceivedEventHandler(ComDevice_DataReceived);
                    dianzuPort.Open();
              
                }
                catch
                { MessageBox.Show("串口打开失败");
                return;
                }

                StartCanAndModbus();
    
        }


        private void ComDevice_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            try
            {
                

                Thread.Sleep(60);
                byte[] ReDatas = new byte[dianzuPort.BytesToRead];//返回命令包
                dianzuPort.Read(ReDatas, 0, ReDatas.Length);//读取数据

                if (rcd.isStartReceive ) //|| AutoModel == 1
                {
                    
                        if (ReDatas.Length > 0)
                        {
                            if (ReDatas[0].ToString("x2").ToUpper() == "AB" && ReDatas[10].ToString("x2").ToUpper() == "AF")
                            {
                                string dataStr = string.Empty;
                                if (ReDatas[2].ToString("x2").ToUpper() != "2E")
                                {
                                    dataStr += ModbusRtu.GetHexadecimalValue(ReDatas[2].ToString("x2").ToUpper());
                                }

                                if (ReDatas[3].ToString("x2").ToUpper() != "2E")
                                {
                                    dataStr += ModbusRtu.GetHexadecimalValue(ReDatas[3].ToString("x2").ToUpper());
                                }
                                else
                                {
                                    dataStr += ".";
                                }

                                if (ReDatas[4].ToString("x2").ToUpper() != "2E")
                                {
                                    dataStr += ModbusRtu.GetHexadecimalValue(ReDatas[4].ToString("x2").ToUpper());
                                }
                                else
                                {
                                    dataStr += ".";
                                }

                                if (ReDatas[5].ToString("x2").ToUpper() != "2E")
                                {
                                    dataStr += ModbusRtu.GetHexadecimalValue(ReDatas[5].ToString("x2").ToUpper());
                                }
                                else
                                {
                                    dataStr += ".";
                                }

                                if (ReDatas[6].ToString("x2").ToUpper() != "2E")
                                {
                                    dataStr += ModbusRtu.GetHexadecimalValue(ReDatas[6].ToString("x2").ToUpper());
                                }

                               
                                    rcd.currentRes = Convert.ToDouble(dataStr);


                                    if (rcd.currentRes > dianZuDown && rcd.currentRes < dianZuUp)
                                    {
                                       

                                      
                                            if (scanControl == 1)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR1（电阻）")
                                                    {
                                                        dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "OK";

                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                        break;
                                                    }
                                                }

                                                scanControl = 2;

                                                sbr1CodeControl = true;
                                                checkResModel.sbr1_value = rcd.currentRes.ToString();
                                                checkResModel.sbr1_check_result = "OK";
                                                //crc.LeftCheckResult += "电阻OK";


                                                //Thread thTemp = new Thread(new ThreadStart(LeftCheckThMethod));
                                                ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                                //thTemp.IsBackground = true;
                                                //thTemp.Start();
                                            }
                                            else if (scanControl == 2)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR2（电阻）")
                                                    {
                                                        dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "OK";

                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                        break;
                                                    }
                                                }

                                                scanControl = 3;
                                                sbr2CodeControl = true;
                                                checkResModel.sbr2_value = rcd.currentRes.ToString();
                                                checkResModel.sbr2_check_result = "OK";
                                                //crc.RightCheckResult += "电阻OK";


                                                //Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                                                ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                                //thTemp.IsBackground = true;
                                                //thTemp.Start();
                                            }
                                            else if (scanControl == 3)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR3（电阻）")
                                                    {
                                                        dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "OK";

                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                        break;
                                                    }
                                                }


                                                sbr3CodeControl = true;
                                                checkResModel.sbr3_value = rcd.currentRes.ToString();
                                                checkResModel.sbr3_check_result = "OK";

                                                if (selectCode.IsCheckDianLiu)
                                                {
                                                    scanControl = 4;
                                                }
                                                else
                                                {
                                                    scanControl = 0;

                                                    if (sbr1Ng)
                                                    {
                                                        button1.Enabled = true;
                                                    }

                                                    if (sbr2Ng)
                                                    {
                                                        button2.Enabled = true;
                                                    }

                                                    checkResBll.Add(checkResModel);
                                                    //checkResultList.Add(crc);
                                                }
                                                
                                               
                                                //crc.RightCheckResult += "电阻OK";


                                                //Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                                                ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                                //thTemp.IsBackground = true;
                                                //thTemp.Start();
                                            }
                                            else if (scanControl == 0)
                                            {
                                                if (checkAgainMode == 1)
                                                {
                                                    for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                    {
                                                        if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR1（电阻）")
                                                        {
                                                            dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                            dgCheck.Rows[i].Cells[2].Value = "OK";

                                                            dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                            break;
                                                        }
                                                    }

                                                    //try
                                                    //{
                                                    //    if (checkResultList.Count > 0)
                                                    //    {
                                                    //        for (int i = 0; i < checkResultList.Count; i++)
                                                    //        {
                                                    //            if (checkResultList[i].PartNum == crc.PartNum)
                                                    //            {
                                                    //                checkResultList[i].Sbr1DianZuValue = rcd.currentRes;
                                                    //                checkResultList[i].Sbr1CheckResult = "OK";
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    //catch
                                                    //{ }

                                                    checkResModel.sbr1_value = rcd.currentRes.ToString();
                                                    checkResModel.sbr1_check_result = "OK";
                                                    checkResBll.Add(checkResModel);
                                                  
                                                    //crc.Sbr1DianZuValue = rcd.currentRes;
                                                    //crc.Sbr1CheckResult = "OK";

                                                    sbr1Ng = false;
                                                    button1.Enabled = false;
                                                }
                                                else if (checkAgainMode == 2)
                                                {
                                                    for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                    {
                                                        if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR2（电阻）")
                                                        {
                                                            dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                            dgCheck.Rows[i].Cells[2].Value = "OK";

                                                            dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                            break;
                                                        }
                                                    }

                                                    //try
                                                    //{
                                                    //    if (checkResultList.Count > 0)
                                                    //    {
                                                    //        for (int i = 0; i < checkResultList.Count; i++)
                                                    //        {
                                                    //            if (checkResultList[i].PartNum == crc.PartNum)
                                                    //            {
                                                    //                checkResultList[i].Sbr2DianZuValue = rcd.currentRes;
                                                    //                checkResultList[i].Sbr2CheckResult = "OK";
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    //catch
                                                    //{ }

                                                    checkResModel.sbr2_value = rcd.currentRes.ToString();
                                                    checkResModel.sbr2_check_result = "OK";
                                                    checkResBll.Add(checkResModel);

                                                    //crc.Sbr2DianZuValue = rcd.currentRes;
                                                    //crc.Sbr2CheckResult = "OK";

                                                    sbr2Ng = false;
                                                    button2.Enabled = false;
                                                }
                                                else if (checkAgainMode == 3)
                                                {
                                                    for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                    {
                                                        if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR3（电阻）")
                                                        {
                                                            dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                            dgCheck.Rows[i].Cells[2].Value = "OK";

                                                            dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                                            break;
                                                        }
                                                    }

                                                    //try
                                                    //{
                                                    //    if (checkResultList.Count > 0)
                                                    //    {
                                                    //        for (int i = 0; i < checkResultList.Count; i++)
                                                    //        {
                                                    //            if (checkResultList[i].PartNum == crc.PartNum)
                                                    //            {
                                                    //                checkResultList[i].Sbr3DianZuValue = rcd.currentRes;
                                                    //                checkResultList[i].Sbr3CheckResult = "OK";
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    //catch
                                                    //{ }

                                                    checkResModel.sbr3_value = rcd.currentRes.ToString();
                                                    checkResModel.sbr3_check_result = "OK";
                                                    checkResBll.Add(checkResModel);

                                                    //crc.Sbr3DianZuValue = rcd.currentRes;
                                                    //crc.Sbr3CheckResult = "OK";

                                                    sbr3Ng = false;
                                                    button3.Enabled = false;
                                                }
                                            }

                                        rcd.isStartReceive = false;
                                        //isStartCheck = true;
                                    }
                                    else
                                    {

                                       

                                        if (scanControl == 1)
                                        {
                                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                                            {
                                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR1（电阻）")
                                                {
                                                    //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                    dgCheck.Rows[i].Cells[2].Value = "NG";
                                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                                    break;
                                                }
                                            }
                                            //label14.Text += "电阻NG";
                                            scanControl = 2;
                                            sbr1CodeControl = true;
                                            //label6.Text = label15.Text;
                                            //label5.Text = "电阻检测异常";
                                            checkResModel.sbr1_value = rcd.currentRes.ToString();
                                            checkResModel.sbr1_check_result = "NG";
                                            //crc.LeftCheckResult += "电阻NG";
                                            sbr1Ng = true;
                                            //label5.Text += "SBR1电阻NG-";
                                            //Thread thTemp = new Thread(new ThreadStart(LeftCheckThMethod));
                                            ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                            //thTemp.IsBackground = true;
                                            //thTemp.Start();
                                        }
                                        else if (scanControl == 2)
                                        {
                                            //label16.Text += "电阻NG";
                                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                                            {
                                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR2（电阻）")
                                                {
                                                    //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                    dgCheck.Rows[i].Cells[2].Value = "NG";
                                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                                    break;
                                                }
                                            }
                                            scanControl = 3;
                                            sbr2CodeControl = true;
                                            checkResModel.sbr2_value = rcd.currentRes.ToString();
                                            checkResModel.sbr2_check_result = "NG";
                                            //crc.RightCheckResult += "电阻NG";

                                            sbr2Ng = true;
                                            //label5.Text += "SBR2电阻NG-";
                                            //Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                                            ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                            //thTemp.IsBackground = true;
                                            //thTemp.Start();
                                        }
                                        else if (scanControl == 3)
                                        {
                                            //label16.Text += "电阻NG";
                                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                                            {
                                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR3（电阻）")
                                                {
                                                    //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                    dgCheck.Rows[i].Cells[2].Value = "NG";
                                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                                    break;
                                                }
                                            }

                                         

                                            sbr3CodeControl = true;
                                            checkResModel.sbr3_value = rcd.currentRes.ToString();
                                            checkResModel.sbr3_check_result = "NG";

                                            sbr3Ng = true;
                                            //label5.Text += "SBR3电阻NG-";
                                            //crc.RightCheckResult += "电阻NG";

                                            if (selectCode.IsCheckDianLiu)
                                            {
                                                scanControl = 4;
                                            }
                                            else
                                            {
                                                scanControl = 0;

                                                if(sbr1Ng)
                                                button1.Enabled = true;

                                                if (sbr2Ng)
                                                    button2.Enabled = true;

                                                button3.Enabled = true;


                                                checkResBll.Add(checkResModel);
                                                
                                            }
                                            //Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                                            ////thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                                            //thTemp.IsBackground = true;
                                            //thTemp.Start();
                                        }
                                        else if (scanControl == 0)
                                        {
                                            if (checkAgainMode == 1)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR1（电阻）")
                                                    {
                                                        //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "NG";
                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Fuchsia;
                                                        break;
                                                    }
                                                }
                                                //label14.Text += "电阻NG";
                                                //label6.Text = label15.Text;
                                                //label5.Text = "电阻检测异常";
                                                //crc.Sbr1DianZuValue = rcd.currentRes;
                                                //crc.Sbr1CheckResult = "NG";
                                                //crc.LeftCheckResult += "电阻NG";
                                                sbr1Ng = true;
                                                button1.Enabled = true;
                                            }
                                            else if (checkAgainMode == 2)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR2（电阻）")
                                                    {
                                                        //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "NG";
                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Fuchsia;
                                                        break;
                                                    }
                                                }
                                                //label14.Text += "电阻NG";
                                                //label6.Text = label15.Text;
                                                //label5.Text = "电阻检测异常";
                                                //crc.Sbr2DianZuValue = rcd.currentRes;
                                                //crc.Sbr2CheckResult = "NG";
                                                //crc.LeftCheckResult += "电阻NG";
                                                sbr2Ng = true;
                                                button2.Enabled = true;
                                            }
                                            else if (checkAgainMode == 3)
                                            {
                                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                                {
                                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "SBR3（电阻）")
                                                    {
                                                        //dgCheck.Rows[i].Cells[1].Value = rcd.currentRes + "Ω";
                                                        dgCheck.Rows[i].Cells[2].Value = "NG";
                                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Fuchsia;
                                                        break;
                                                    }
                                                }
                                                //label14.Text += "电阻NG";
                                                //label6.Text = label15.Text;
                                                //label5.Text = "电阻检测异常";
                                                //crc.Sbr3DianZuValue = rcd.currentRes;
                                                //crc.Sbr3CheckResult = "NG";
                                                //crc.LeftCheckResult += "电阻NG";
                                                sbr3Ng = true;
                                                button3.Enabled = true;
                                            }
                                        }


                                        rcd.isStartReceive = false;

                                    }


                                    toolStripButton3.Enabled = true;
                               
                             
                            }
                        }


                   
                }
           }
            catch (Exception ex)
            {
                //log.Error(ex.Message);
            }
        }

        ///// <summary>
        ///// 获取接收数据事件方法
        ///// </summary>
        ///// <param name="receiveBytes"></param>
        //public void GetReceiveData(byte[] receiveBytes)
        //{
            

        //    //return tempD;
        //}

        private void StartCanAndModbus()
        {
            //if (cmb_portname.Items.Count <= 0)
            //{
            //    MessageBox.Show("无可用串口");
            //    return;
            //}

            //InitSerialPortParameter();

            ModbusRtu.master = ModbusSerialMaster.CreateRtu(dianliuPort);

            ConnectUsbCan();

            StartUsbCan();

            //thCheck.Start();
        }

   

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;

            this.WindowState = FormWindowState.Maximized;

            //string[] ports = SerialPort.GetPortNames();
            //cmb_portname.Items.AddRange(ports);
            //comboBox1.Items.AddRange(ports);

            //if (cmb_portname.Items.Count > 0)
            //    cmb_portname.SelectedItem = cmb_portname.Items[0];

            //if (comboBox1.Items.Count > 0)
            //    comboBox1.SelectedItem = comboBox1.Items[0];
           

            //cmb_baud.SelectedIndex = 5;
            //cmb_parity.SelectedIndex = 2;
            //cmb_databBits.SelectedIndex = 1;
            //cmb_stopBits.SelectedIndex = 0;
        }
        private CodeClass selectCode;
        //CheckResultClass crc;
        bool leftCodeControl = true;
        bool rightCodeControl = true;
        bool sbr1CodeControl = true;
        bool sbr2CodeControl = true;
        bool sbr3CodeControl = true;


        bool sbr1Ng = false;
        bool sbr2Ng = false;
        bool sbr3Ng = false;
        bool leftNg = false;
        bool rightNg = false;

        //bool dianLiuCheckBool = true;

        private void Listener_ScanerEvent(ScanerHook.ScanerCodes codes)
        {

            if (scanControl == 0)
            {
                label12.Text = codes.Result;
                label15.Text = "--";
                label19.Text = "--";
                label13.Text = "--";
                label25.Text = "--";
                label22.Text = "--";
                label21.Text = "--";
                label17.Text = "--";
                label16.Text = "--";
                label18.Text = "--";
                label14.Text = "--";
                label20.Text = "--";


                label15.ForeColor = Color.Lime;
                label19.ForeColor = Color.Lime;
                label25.ForeColor = Color.Lime;
                label22.ForeColor = Color.Lime;
                label21.ForeColor = Color.Lime;
                label17.ForeColor = Color.Lime;
                label16.ForeColor = Color.Lime;
                label18.ForeColor = Color.Lime;
                label14.ForeColor = Color.Lime;
                label20.ForeColor = Color.Lime;

                label3.ForeColor = Color.Lime;
                label26.ForeColor = Color.Lime;
                label4.ForeColor = Color.Lime;
                label24.ForeColor = Color.Lime;
                label23.ForeColor = Color.Lime;

                sbr1CodeControl = true;
                sbr2CodeControl = true;
                sbr3CodeControl = true;
                leftCodeControl = true;
                rightCodeControl = true;

                sbr1Ng = false;
                sbr2Ng = false;
                sbr3Ng = false;
                leftNg = false;
                rightNg = false;

                //label5.Text = "";
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                checkAgainMode = 0;

                if (ruleList.Count == 0)
                {
                    MessageBox.Show("无规则可用");
                    return;
                }
                else
                {
                    for (int i = 0; i < ruleList.Count; i++)
                    {
                        int tempLength = 0;
                        string tempId = string.Empty;
                        int startAdd = 0;
                        int subLength = 0;
                        try
                        {
                            tempLength = int.Parse(ruleList[i].Split(';')[0]);
                            tempId = ruleList[i].Split(';')[1];

                            startAdd = int.Parse(tempId.Split('-')[0]);
                            subLength = int.Parse(tempId.Split('-')[1]);
                        }
                        catch
                        {
                            MessageBox.Show("规则异常，请检查字符");
                            continue;
                        }


                        if (tempLength == codes.Result.Length)
                        {
                            string tempPartId = codes.Result.Substring(startAdd - 1, subLength - startAdd + 1);


                            foreach (Model.Check_Product item in checkProList)
                            {
                                if (tempPartId.ToUpper() == item.part_id)
                                {
                                    label13.Text = "匹配成功";
                                    label12.Text = item.part_id;
                                    label2.ForeColor = Color.Lime;
                                    label12.ForeColor = Color.Lime;
                                    label13.ForeColor = Color.Lime;

                                    checkResModel = new Model.Check_Result();

                                    checkResModel.vehicle_name = item.vehicle_type;
                                    checkResModel.seat_loc = item.seat_loc;
                                    checkResModel.part_id = item.part_id;

                                    //crc = new CheckResultClass();

                                    //crc.VehicleName = item.vehicle_type;
                                    //crc.SeatName = item.seat_loc;
                                    //crc.PartNum = item.part_id;
                                    //crc.TotalCode = codeDic[item].TotalCode;

                                    if (item.is_check_dianliu)
                                    {
                                        checkResModel.left_code = item.left_code;
                                        checkResModel.right_code = item.right_code;

                                        //dianLiuCheckBool = true;
                                    }
                                    else
                                    {
                                        checkResModel.left_code = "--";
                                        checkResModel.right_code = "--";
                                        checkResModel.left_dianliu_value = "--";
                                        checkResModel.left_check_result = "--";
                                        checkResModel.right_dianliu_value = "--";
                                        checkResModel.right_check_result = "--";
                                        checkResModel.left_check_starttime = DateTime.Now;
                                        checkResModel.left_check_endtime = DateTime.Now;
                                        checkResModel.right_check_starttime = DateTime.Now;
                                        checkResModel.right_check_endtime = DateTime.Now;
                                        //dianLiuCheckBool = false;

                                        label14.Text = "无需测试";
                                        label18.Text = "无需测试";
                                    }


                                    if (item.is_check_dianzu)
                                    {
                                        checkResModel.sbr1_code = item.sbr1_code;
                                        checkResModel.sbr2_code = item.sbr2_code;
                                        checkResModel.sbr3_code = item.sbr3_code;
                                    }
                                    else
                                    {
                                        checkResModel.sbr1_code = "--";
                                        checkResModel.sbr2_code = "--";
                                        checkResModel.sbr3_code = "--";
                                        checkResModel.sbr1_value = "--";
                                        checkResModel.sbr2_value = "--";
                                        checkResModel.sbr3_value = "--";
                                        checkResModel.sbr1_check_result = "--";
                                        checkResModel.sbr2_check_result = "--";
                                        checkResModel.sbr3_check_result = "--";

                                        label19.Text = "无需测试";
                                        label16.Text = "无需测试";
                                        label20.Text = "无需测试";

                                    }

                                    scanControl = 1;

                                    selectCode = new CodeClass();

                                    selectCode.VehicleName = item.vehicle_type;
                                    selectCode.SeatName = item.seat_loc;
                                    selectCode.PartNum = item.part_id;
                                    selectCode.LeftCode = item.left_code;
                                    selectCode.RightCode = item.right_code;
                                    selectCode.Sbr1Code = item.sbr1_code;
                                    selectCode.Sbr2Code = item.sbr2_code;
                                    selectCode.Sbr3Code = item.sbr3_code;
                                    selectCode.IsCheckDianLiu = item.is_check_dianliu;
                                    selectCode.IsCheckDianZu = item.is_check_dianzu;

                                    return;
                                }
                            }
                        }
                    }
                }



                label13.Text = "匹配不成功";
                label2.ForeColor = Color.Red;
                label12.ForeColor = Color.Red;
                label13.ForeColor = Color.Red;


            }
            else if (scanControl == 1)
            {
                if (selectCode.IsCheckDianZu)
                {
                    if (sbr1CodeControl)
                    {
                        label25.Text = codes.Result;
                        if (label25.Text.Contains(selectCode.Sbr1Code.ToUpper())|| label25.Text.Contains(selectCode.Sbr1Code.ToLower()))
                        {
                            label19.Text = "匹配成功";
                            label19.ForeColor = Color.Lime;
                            label25.ForeColor = Color.Lime;
                            label26.ForeColor = Color.Lime;

                            dgCheck.Rows.Clear();
                            dgCheck.ReadOnly = true;
                            sbr1CodeControl = false;

                            UpdateCheck();

                            rcd = new ResCodeData();
                            rcd.isStartReceive = true;
                            //发送电阻检测
                            //toolStripButton3.Enabled = false;

                            DianZuTest();
                        }
                        else
                        {
                            label19.Text = "匹配不成功";
                            label19.ForeColor = Color.Red;
                            label25.ForeColor = Color.Red;
                            label26.ForeColor = Color.Red;
                        }
                    }
                }
                else if (!selectCode.IsCheckDianZu && selectCode.IsCheckDianLiu)
                {

                    if (leftCodeControl)
                    {
                        label15.Text = codes.Result;
                        if (label15.Text.Contains(selectCode.LeftCode.ToUpper())|| label15.Text.Contains(selectCode.LeftCode.ToLower()))
                        {
                            label14.Text = "匹配成功";
                            label3.ForeColor = Color.Lime;
                            label14.ForeColor = Color.Lime;
                            label15.ForeColor = Color.Lime;

                            dgCheck.Rows.Clear();
                            dgCheck.ReadOnly = true;
                            leftCodeControl = false;

                            UpdateCheckDianliu(1);

                            ecd = new EleCodeData();
                            //rcd = new ResCodeData();
                            //rcd.isStartReceive = true;
                            //发送电阻检测
                            //toolStripButton3.Enabled = false;
                            //DianZuTest();

                            Thread thTemp = new Thread(new ThreadStart(LeftCheckThMethod));
                            //thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                            thTemp.IsBackground = true;
                            thTemp.Start();


                            checkResModel.left_check_starttime = DateTime.Now;

                        }
                        else
                        {
                            label14.Text = "匹配不成功";
                            label3.ForeColor = Color.Red;
                            label14.ForeColor = Color.Red;
                            label15.ForeColor = Color.Red;
                        }
                    }



                }
            }
            else if (scanControl == 2)
            {
                if (selectCode.IsCheckDianZu)
                {
                    if (sbr2CodeControl)
                    {
                        label22.Text = codes.Result;
                        if (label22.Text.Contains(selectCode.Sbr2Code.ToUpper())|| label22.Text.Contains(selectCode.Sbr2Code.ToLower()))
                        {
                            label16.Text = "匹配成功";
                            label16.ForeColor = Color.Lime;
                            label22.ForeColor = Color.Lime;
                            label24.ForeColor = Color.Lime;


                            sbr2CodeControl = false;


                            rcd = new ResCodeData();
                            rcd.isStartReceive = true;
                            //发送电阻检测
                            //toolStripButton3.Enabled = false;

                            DianZuTest();
                        }
                        else
                        {
                            label16.Text = "匹配不成功";
                            label16.ForeColor = Color.Red;
                            label22.ForeColor = Color.Red;
                            label24.ForeColor = Color.Red;
                        }
                    }
                }
                else if (!selectCode.IsCheckDianZu && selectCode.IsCheckDianLiu)
                {

                    if (rightCodeControl)
                    {

                        label17.Text = codes.Result;
                        if (label17.Text.Contains(selectCode.RightCode.ToUpper()) || label17.Text.Contains(selectCode.RightCode.ToLower()))
                        {
                            label18.Text = "匹配成功";
                            label4.ForeColor = Color.Lime;
                            label17.ForeColor = Color.Lime;
                            label18.ForeColor = Color.Lime;


                            rightCodeControl = false;

                            UpdateCheckDianliu(2);

                            ecd = new EleCodeData();
                            //toolStripButton3.Enabled = false;

                            Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                            thTemp.IsBackground = true;
                            thTemp.Start();


                            checkResModel.right_check_starttime = DateTime.Now;

                        }
                        else
                        {
                            label18.Text = "匹配不成功";
                            label4.ForeColor = Color.Red;
                            label17.ForeColor = Color.Red;
                            label18.ForeColor = Color.Red;
                        }


                    }
                }
            }
            else if (scanControl == 3)
            {
                if (sbr3CodeControl)
                {
                    label21.Text = codes.Result;
                    if (label21.Text.Contains(selectCode.Sbr3Code.ToUpper()) || label21.Text.Contains(selectCode.Sbr3Code.ToLower()))
                    {
                        label20.Text = "匹配成功";
                        label20.ForeColor = Color.Lime;
                        label21.ForeColor = Color.Lime;
                        label23.ForeColor = Color.Lime;


                        sbr3CodeControl = false;


                        rcd = new ResCodeData();
                        rcd.isStartReceive = true;
                        //发送电阻检测
                        //toolStripButton3.Enabled = false;
                        DianZuTest();
                    }
                    else
                    {
                        label20.Text = "匹配不成功";
                        label20.ForeColor = Color.Red;
                        label21.ForeColor = Color.Red;
                        label23.ForeColor = Color.Red;
                    }
                }
            }
            else if (scanControl == 4)
            {
                if (leftCodeControl)
                {
                    label15.Text = codes.Result;
                    if (label15.Text.Contains(selectCode.LeftCode.ToUpper()) || label15.Text.Contains(selectCode.LeftCode.ToLower()))
                    {
                        label14.Text = "匹配成功";
                        label3.ForeColor = Color.Lime;
                        label14.ForeColor = Color.Lime;
                        label15.ForeColor = Color.Lime;


                        leftCodeControl = false;

                        UpdateCheckDianliu(1);

                        ecd = new EleCodeData();
                        //rcd = new ResCodeData();
                        //rcd.isStartReceive = true;
                        //发送电阻检测
                        //DianZuTest();
                        //toolStripButton3.Enabled = false;
                        Thread thTemp = new Thread(new ThreadStart(LeftCheckThMethod));
                        //thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                        thTemp.IsBackground = true;
                        thTemp.Start();


                        checkResModel.left_check_starttime = DateTime.Now;

                    }
                    else
                    {
                        label14.Text = "匹配不成功";
                        label3.ForeColor = Color.Red;
                        label14.ForeColor = Color.Red;
                        label15.ForeColor = Color.Red;
                    }

                }
            }
            else if (scanControl == 5)
            {
                if (rightCodeControl)
                {


                    label17.Text = codes.Result;
                    if (label17.Text.Contains(selectCode.RightCode.ToUpper()) || label17.Text.Contains(selectCode.RightCode.ToLower()))
                    {
                        label18.Text = "匹配成功";
                        label4.ForeColor = Color.Lime;
                        label17.ForeColor = Color.Lime;
                        label18.ForeColor = Color.Lime;


                        rightCodeControl = false;

                        UpdateCheckDianliu(2);

                        ecd = new EleCodeData();
                        //toolStripButton3.Enabled = false;

                        Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                        thTemp.IsBackground = true;
                        thTemp.Start();


                        checkResModel.right_check_starttime = DateTime.Now;

                    }
                    else
                    {
                        label18.Text = "匹配不成功";
                        label4.ForeColor = Color.Red;
                        label17.ForeColor = Color.Red;
                        label18.ForeColor = Color.Red;
                    }


                }
            }




        }


        //测试，不用可以注释
        //private void StartTest()
        //{
        //    if (AutoControl)
        //    {
        //        string tempStr = "1111";


        //        foreach (string item in codeDic.Keys)
        //        {
        //            if (codeDic[item].LeftCode == tempStr)
        //            {
        //                label5.Text = "左码值：" + tempStr;
        //                AutoLeftRight = 0;

        //                AutoControl = false;
        //                HandC = false;
        //                UpdateCheck();

        //                rcd = new ResCodeData();

        //                DianZuTest();
        //                return;
        //            }
        //            else if (codeDic[item].RightCode == tempStr)
        //            {
        //                label5.Text = "右码值：" + tempStr;
        //                AutoLeftRight = 1;

        //                AutoControl = false;
        //                HandC = false;
        //                UpdateCheck();

        //                rcd = new ResCodeData();

        //                DianZuTest();
        //                return;
        //            }
        //        }

        //        MessageBox.Show("条码不存在请事先录入");
        //    }
        //}


        private void UpdateCheck()
        {
           
            for (int i = 0; i < 3; i++)
            {
                int index = dgCheck.Rows.Add();
                if (i == 0)
                {
                    dgCheck.Rows[index].Cells[0].Value = "SBR1（电阻）";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 1)
                {
                    dgCheck.Rows[index].Cells[0].Value = "SBR2（电阻）";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 2)
                {
                    dgCheck.Rows[index].Cells[0].Value = "SBR3（电阻）";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                //else if (i == 2)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(中档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "2.44A";
                //    dgCheck.Rows[index].Cells[2].Value = "OK";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                //}
                //else if (i == 3)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(低档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "0A";
                //    dgCheck.Rows[index].Cells[2].Value = "NG";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
                //}


            }


            //dgCheck.ClearSelection();

        }


        private void UpdateCheckDianliu(int mode)
        {
            //dgCheck.Rows.Clear();

            
            //for (int i = 0; i < 20; i++)
            //{
                int index = dgCheck.Rows.Add();
               
                if (mode == 1)
                {
                    dgCheck.Rows[index].Cells[0].Value = "左加热垫（电流）";

                }
                else
                {
                    dgCheck.Rows[index].Cells[0].Value = "右加热垫（电流）";

                }
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                //}
                //else if (i == 2)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(中档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "2.44A";
                //    dgCheck.Rows[index].Cells[2].Value = "OK";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                //}
                //else if (i == 3)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(低档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "0A";
                //    dgCheck.Rows[index].Cells[2].Value = "NG";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
                //}

                    //dgCheck.ClearSelection();
           // }

        }
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            listener.Stop();
            //CloseExcel();
        }

        public void ConnectUsbCan()
        {
            if (m_bOpen == 1)
            {
                VCI_CloseDevice(m_devtype, m_devind);
                m_bOpen = 0;
            }
            else
            {
                m_devtype = 4;

                m_devind = 0;
                m_canind = 0;
                if (VCI_OpenDevice(m_devtype, m_devind, 0) == 0)
                {
                    MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                m_bOpen = 1;
                VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();
                config.AccCode = System.Convert.ToUInt32("0xFB200000", 16);
                config.AccMask = System.Convert.ToUInt32("0x600000", 16);
                config.Timing0 = System.Convert.ToByte("0x00", 16);
                config.Timing1 = System.Convert.ToByte("0x1C", 16);
                config.Filter = (byte)2;
                config.Mode = (byte)0;
                VCI_InitCAN(m_devtype, m_devind, m_canind, ref config);
            }
            //button5.Text = m_bOpen == 1 ? "断开" : "连接";
            timer1.Enabled = m_bOpen == 1 ? true : false;
        }

        public void StartUsbCan()
        {
            if (m_bOpen == 0)
                return;
            VCI_StartCAN(m_devtype, m_devind, m_canind);
        }

        unsafe private void SendRightCanData()
        {
            if (m_bOpen == 0)
            {
                MessageBox.Show("CAN模块未连接成功");
                return;
            }

            try
            {
                for (int i = 0; i < 4; i++)
                {
                    VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();
                    //sendobj.Init();
                    sendobj.RemoteFlag = (byte)0;
                    sendobj.ExternFlag = (byte)0;
                    sendobj.ID = System.Convert.ToUInt32("0x000007C9", 16);
                    sendobj.DataLen = (byte)8;
                    if (i == 0)
                    {
                        String strdata = "02 10 01 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 1)
                    {
                        String strdata = "02 10 03 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 2)
                    {
                        String strdata = "02 27 09 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 3)
                    {
                        String strdata = "05 2F 79 66 03 02 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }

                    VCI_Transmit(m_devtype, m_devind, m_canind, ref sendobj, 1);

                    Thread.Sleep(100);
                }
            }
            catch
            { }
         
           
        }

        unsafe private void SendLeftCanData()
        {
            if (m_bOpen == 0)
            {
                MessageBox.Show("CAN模块未连接成功");
                return;
            }

            try
            {
                for (int i = 0; i < 4; i++)
                {
                    VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();
                    //sendobj.Init();
                    sendobj.RemoteFlag = (byte)0;
                    sendobj.ExternFlag = (byte)0;
                    sendobj.ID = System.Convert.ToUInt32("0x000007C7", 16);
                    sendobj.DataLen = (byte)8;
                    if (i == 0)
                    {
                        String strdata = "02 10 01 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 1)
                    {
                        String strdata = "02 10 03 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 2)
                    {
                        String strdata = "02 27 09 00 00 00 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }
                    else if (i == 3)
                    {
                        String strdata = "05 2F 79 66 03 02 00 00";
                        int j = -1;
                        if (j++ < 7)
                            sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);
                        if (j++ < 7)
                            sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(j * 3, 2), 16);

                    }

                    VCI_Transmit(m_devtype, m_devind, m_canind, ref sendobj, 1);

                    Thread.Sleep(100);
                }
            }
            catch
            { }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            //SendCanData();
            DialogResult dr = MessageBox.Show("是否复检？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                try
                {
                    checkAgainMode = 3;

                    rcd = new ResCodeData();
                    rcd.isStartReceive = true;
                    //发送电阻检测

                    DianZuTest();
                }
                catch
                { }
            }
        }

       
        unsafe private void timer1_Tick(object sender, EventArgs e)
        {
            UInt32 res = new UInt32();

            res = VCI_Receive(m_devtype, m_devind, m_canind, ref m_recobj[0], 1000, 100);

            /////////////////////////////////////
            //IntPtr[] ptArray = new IntPtr[1];
            //ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * 50);
            //IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * 1);

            //Marshal.Copy(ptArray, 0, pt, 1);


            //res = VCI_Receive(m_devtype, m_devind, m_canind, pt, 50/*50*/, 100);
            ////////////////////////////////////////////////////////
            if (res == 0xFFFFFFFF) res = 0;//当设备未初始化时，返回0xFFFFFFFF，不进行列表显示。
            String str = "";
            for (UInt32 i = 0; i < res; i++)
            {
                //VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(VCI_CAN_OBJ))), typeof(VCI_CAN_OBJ));

                str = "接收到数据: ";

                str += "  帧ID:0x" + System.Convert.ToString(m_recobj[i].ID, 16);
                str += "  帧格式:";
                if (m_recobj[i].RemoteFlag == 0)
                    str += "数据帧 ";
                else
                    str += "远程帧 ";
                if (m_recobj[i].ExternFlag == 0)
                    str += "标准帧 ";
                else
                    str += "扩展帧 ";

                //////////////////////////////////////////
                if (m_recobj[i].RemoteFlag == 0)
                {
                    str += "数据: ";
                    byte len = (byte)(m_recobj[i].DataLen % 9);
                    byte j = 0;
                    fixed (VCI_CAN_OBJ* m_recobj1 = &m_recobj[i])
                    {
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[0], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[1], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[2], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[3], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[4], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[5], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[6], 16);
                        if (j++ < len)
                            str += " " + System.Convert.ToString(m_recobj1->Data[7], 16);
                    }
                }

                //listBox_Info.Items.Add(str);
                //listBox_Info.SelectedIndex = listBox_Info.Items.Count - 1;
            }
        }

        int checkAgainMode = 0;

        private void button5_Click(object sender, EventArgs e)
        {

            DialogResult dr = MessageBox.Show("是否复检？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                try
                {
                    checkAgainMode = 5;
                    ecd = new EleCodeData();

                    Thread thTemp = new Thread(new ThreadStart(RightCheckThMethod));
                    thTemp.IsBackground = true;
                    thTemp.Start();


                    //crc.RightCheckTime = DateTime.Now;
                }
                catch
                { }

            }//ConnectUsbCan();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            StartUsbCan();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否复检？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                try
                {
                    checkAgainMode = 2;

                    rcd = new ResCodeData();
                    rcd.isStartReceive = true;
                    //发送电阻检测

                    DianZuTest();
                }
                catch
                { }
            }
           
        }


        private void LeftCheckThMethod()
        {
            try
            {
                //while (true)//!cd.isQualified&&
                //{
                        //if (textBox6.Text.Length != 0 && textBox6.Text != "" && textBox6.Text != null && textBox6.Text != string.Empty)
                        //{
                        //    if (textBox6.Text.Length < 20)//电流
                        //    {
                    //if (isStartCheck)
                    //{
                        SendLeftCanData();
                        //ecd.eleCodeStr = textBox6.Text;
                        //ecd.currentEle = 0;
                        //ecd.checkMode = 0;//电流
                        //ecd.checkTime = DateTime.Now;
                        //ecd.isQualified = false;
                        //ecd.isOut = false;

                      
                        dianList.Clear();

                      
                            Thread dataTh = new Thread(new ThreadStart(DataThMethod));
                            dataTh.IsBackground = true;
                            dataTh.Start();

                            Thread timeTh = new Thread(new ThreadStart(TimeThMethod));
                            timeTh.IsBackground = true;
                            timeTh.Start();

                       
            }
            catch
            { }



        }

        private void RightCheckThMethod()
        {
            try
            {
                //while (true)//!cd.isQualified&&
                //{
                //if (textBox6.Text.Length != 0 && textBox6.Text != "" && textBox6.Text != null && textBox6.Text != string.Empty)
                //{
                //    if (textBox6.Text.Length < 20)//电流
                //    {
                //if (isStartCheck)
                //{
                SendRightCanData();
                //ecd.eleCodeStr = textBox6.Text;
                //ecd.currentEle = 0;
                //ecd.checkMode = 0;//电流
                //ecd.checkTime = DateTime.Now;
                //ecd.isQualified = false;
                //ecd.isOut = false;


                dianList.Clear();

               
                    Thread dataTh = new Thread(new ThreadStart(DataThMethod));
                    dataTh.IsBackground = true;
                    dataTh.Start();

                    Thread timeTh = new Thread(new ThreadStart(TimeThMethod));
                    timeTh.IsBackground = true;
                    timeTh.Start();
              


                //isStartCheck = false;

                //textBox6.Text = "";
                //}                              
                //    }
                //    else
                //    {
                //        if (isStartCheck)
                //        {
                //            rcd.resCodeStr = textBox6.Text;
                //            rcd.currentRes = 0;
                //            //ecd.checkMode = 0;//电流
                //            rcd.checkTime = DateTime.Now;
                //            rcd.isQualified = false;
                //            rcd.isStartReceive = true;


                //            isStartCheck = false;

                //            textBox6.Text = "";

                //            //发送指令
                //            byte[] b = { 0xAB, 0x9D, 0xAF };
                //            port232.Write(b, 0, b.Length);

                //        }
                //    }                                                    
                //}
                //}
            }
            catch
            { }



        }

        private void DataThMethod()
        {
            try
            {
                while (!ecd.isOut)//!cd.isQualified&&
                {
                    if (dianliuPort.IsOpen == false)
                    {
                        dianliuPort.Open();
                    }


                    try
                    {
                        registerBuffer = ModbusRtu.ReadHoldingRegister(3, 1, 0, 1);

                        //int ele = ModbusRtu.GetHexadecimalValue(registerBuffer[0].ToString());

                        int ele = int.Parse(registerBuffer[0].ToString());

                        double eled = (double)ele / 1000;


                        if (!dianList.Contains(eled))
                        {
                            dianList.Add(eled);
                        }
                    }
                    catch
                    { }

                    Thread.Sleep(100);                  
                }

                if (ecd.isOut)
                {
                    if (dianList.Max() > dianLiuMidDown && dianList.Max() < dianLiuMidUp)
                    {
                        ecd.currentEle = dianList.Max();



                        if (scanControl == 1)
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                {
                                    dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                    break;
                                }
                            }

                            scanControl = 2;

                            leftCodeControl = true;

                             checkResModel.left_dianliu_value = ecd.currentEle.ToString();
                             checkResModel.left_check_result = "OK";
                             checkResModel.left_check_endtime = DateTime.Now;

                        }
                        else if (scanControl == 4)
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                {
                                    dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                    break;
                                }
                            }

                            scanControl = 5;

                            leftCodeControl = true;

                            checkResModel.left_dianliu_value = ecd.currentEle.ToString();
                            checkResModel.left_check_result = "OK";
                            checkResModel.left_check_endtime = DateTime.Now;

                        }
                        else if (scanControl == 2 || scanControl == 5) 
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "右加热垫（电流）")
                                {
                                    dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                    break;
                                }
                            }


                            if (scanControl == 2)
                            {
                                if(leftNg)
                                button4.Enabled = true;
                            }
                            else if (scanControl == 5)
                            {
                                if (sbr1Ng)
                                    button1.Enabled = true;

                                if (sbr2Ng)
                                    button2.Enabled = true;

                                if ( sbr3Ng)
                                    button3.Enabled = true;

                                if (leftNg)
                                    button4.Enabled = true;
                            }

                            scanControl = 0;

                            rightCodeControl = true;


                            checkResModel.right_dianliu_value = ecd.currentEle.ToString();
                            checkResModel.right_check_result = "OK";
                            checkResModel.right_check_endtime = DateTime.Now;

                            checkResBll.Add(checkResModel);

                           
                        }
                        else if (scanControl == 0)
                        {
                            if (checkAgainMode == 4)
                            {
                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                {
                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                    {
                                        dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                        dgCheck.Rows[i].Cells[2].Value = "OK";

                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                        break;
                                    }
                                }

                                //try
                                //{
                                //    if (checkResultList.Count > 0)
                                //    {
                                //        for (int i = 0; i < checkResultList.Count; i++)
                                //        {
                                //            if (checkResultList[i].PartNum == crc.PartNum)
                                //            {
                                //                checkResultList[i].LeftDianLiuValue = ecd.currentEle;
                                //                checkResultList[i].LeftCheckResult = "OK";
                                //                //checkResultList[i].LeftFinishTime = DateTime.Now;
                                //            }
                                //        }
                                //    }
                                //}
                                //catch
                                //{ }

                                checkResModel.left_dianliu_value = ecd.currentEle.ToString();
                                checkResModel.left_check_result = "OK";
                                checkResModel.left_check_endtime = DateTime.Now;
                                //crc.LeftDianLiuValue = ecd.currentEle;
                                //crc.LeftCheckResult = "OK";
                                //crc.LeftFinishTime = DateTime.Now;

                                leftNg = false;
                                button4.Enabled = false;
                            }
                            else if (checkAgainMode == 5)
                            {
                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                {
                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "右加热垫（电流）")
                                    {
                                        dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                        dgCheck.Rows[i].Cells[2].Value = "OK";

                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                        break;
                                    }
                                }

                                //try
                                //{
                                //    if (checkResultList.Count > 0)
                                //    {
                                //        for (int i = 0; i < checkResultList.Count; i++)
                                //        {
                                //            if (checkResultList[i].PartNum == crc.PartNum)
                                //            {
                                //                checkResultList[i].RightDianLiuValue = ecd.currentEle;
                                //                checkResultList[i].RightCheckResult = "OK";
                                //                //checkResultList[i].LeftFinishTime = DateTime.Now;
                                //            }
                                //        }
                                //    }
                                //}
                                //catch
                                //{ }

                                checkResModel.right_dianliu_value = ecd.currentEle.ToString();
                                checkResModel.right_check_result = "OK";
                                checkResModel.right_check_endtime = DateTime.Now;
                                //crc.RightDianLiuValue = ecd.currentEle;
                                //crc.RightCheckResult = "OK";
                                //crc.RightFinishTime = DateTime.Now;

                                rightNg = false;
                                button5.Enabled = false;
                            }
                        }
                       

                 
                        ecd.isOut = false;

                    }
                    else
                    {
                     
                        if (scanControl == 1)
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                {
                                    //dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;

                                    break;
                                }
                            }

                         
                            scanControl = 2;

                            leftCodeControl = true;

                            checkResModel.left_dianliu_value = ecd.currentEle.ToString();
                            checkResModel.left_check_result = "NG";
                            checkResModel.left_check_endtime = DateTime.Now;

                            leftNg = true;
                            //label5.Text += "左加热垫电流NG-";
                        }
                        else if (scanControl == 4)
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                {
                                    //dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;

                                    break;
                                }
                            }


                            scanControl = 5;

                            leftCodeControl = true;

                            checkResModel.left_dianliu_value = ecd.currentEle.ToString();
                            checkResModel.left_check_result = "NG";
                            checkResModel.left_check_endtime = DateTime.Now;

                            leftNg = true;
                            //label5.Text += "左加热垫电流NG-";
                        }
                        else if (scanControl == 2||scanControl==5)
                        {
                            for (int i = 0; i < dgCheck.Rows.Count; i++)
                            {
                                if (dgCheck.Rows[i].Cells[0].Value.ToString() == "右加热垫（电流）")
                                {
                                    //dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;

                                    break;
                                }
                            }

                            

                            rightCodeControl = true;

                            checkResModel.right_dianliu_value = ecd.currentEle.ToString();
                            checkResModel.right_check_result = "NG";
                            checkResModel.right_check_endtime = DateTime.Now;

                            rightNg = true;
                            //label5.Text += "右加热垫电流NG-";

                            if (scanControl == 2)
                            {
                                if (leftNg)
                                    button4.Enabled = true;
                            }
                            else if (scanControl == 5)
                            {
                                if (sbr1Ng)
                                    button1.Enabled = true;

                                if (sbr2Ng)
                                    button2.Enabled = true;

                                if (sbr3Ng)
                                    button3.Enabled = true;

                                if (leftNg)
                                    button4.Enabled = true;
                            }

                            button5.Enabled = true;

                            scanControl = 0;
                            checkResBll.Add(checkResModel);

                        }
                        else if (scanControl == 0)
                        {
                            if (checkAgainMode == 4)
                            {
                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                {
                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "左加热垫（电流）")
                                    {
                                        //dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                        dgCheck.Rows[i].Cells[2].Value = "NG";

                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Fuchsia;

                                        break;
                                    }
                                }

                              

                                //crc.LeftDianLiuValue = ecd.currentEle;
                                //crc.LeftCheckResult = "OK";
                                //crc.LeftFinishTime = DateTime.Now;

                                leftNg = true;
                                button4.Enabled = true;
                            }
                            else if (checkAgainMode == 5)
                            {
                                for (int i = 0; i < dgCheck.Rows.Count; i++)
                                {
                                    if (dgCheck.Rows[i].Cells[0].Value.ToString() == "右加热垫（电流）")
                                    {
                                        //dgCheck.Rows[i].Cells[1].Value = ecd.currentEle + "A";
                                        dgCheck.Rows[i].Cells[2].Value = "NG";

                                        dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Fuchsia;

                                        break;
                                    }
                                }



                                //crc.LeftDianLiuValue = ecd.currentEle;
                                //crc.LeftCheckResult = "OK";
                                //crc.LeftFinishTime = DateTime.Now;

                                rightNg = true;
                                button5.Enabled = true;
                            }
                        }

                        ecd.isOut = false;

                        //isStartCheck = true;
                    }


                  
                }


                toolStripButton3.Enabled = true;
            }
            catch
            { }



        }

        private void TimeThMethod()
        {
            Thread.Sleep(7000);
            ecd.isOut = true;
        }

   

        #region Excel处理
        //Microsoft.Office.Interop.Excel.Workbook xBook;
        //Microsoft.Office.Interop.Excel.Worksheet xSheet;
        //Microsoft.Office.Interop.Excel.Application xApp;

        //Microsoft.Office.Interop.Excel.Workbook xBookPass;
        //Microsoft.Office.Interop.Excel.Application xAppPass;

        //public void OpenExcel()
        //{
        //    try
        //    {
        //        OpenFileDialog ofd = new OpenFileDialog();
        //        ofd.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
        //        ofd.InitialDirectory = targetPath;
        //        ofd.ValidateNames = true;
        //        ofd.CheckPathExists = true;
        //        ofd.CheckFileExists = true;

        //        string strFileFullPath = targetPath;
        //        xApp = new Microsoft.Office.Interop.Excel.Application();
        //        xApp.Visible = true;
        //        //xApp.UserControl = true;

        //        xBook = xApp.Workbooks.Open(strFileFullPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        //        xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
        //        if (xSheet == null)
        //        {
        //            return;
        //        }
        //        //this.button3.Enabled = false;
        //    }



        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Excel文件路径不存在！！！", "错误");

        //        return;
        //    }
        //}

        ///// <summary>
        ///// 写入excel
        ///// </summary>
        ///// <param name="dataStr"></param>
        //public void WriteToExcel(EleCodeData cd)
        //{
        //    try
        //    {


        //        //int countTemp = 0;

        //        //do
        //        //{
        //        //    countTemp++;
        //        //} while (((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != "" && ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != null);
        //        ////count = countTemp;
        //        //xSheet.Cells[countTemp, 1] = cd.eleCodeStr;
        //        //xSheet.Cells[countTemp, 2] = cd.currentEle+"A";
        //        //if(cd.isQualified)
        //        //xSheet.Cells[countTemp, 3] = "合格";
        //        //else
        //        //    xSheet.Cells[countTemp, 3] = "不合格";

        //        ////if (cd.checkMode==0)
        //        //    xSheet.Cells[countTemp, 4] = "电流检测";
               

        //        //xSheet.Cells[countTemp, 5] = cd.checkTime;

        //        ////count++;


        //    }
        //    catch
        //    { }

        //}


        //public void WriteToExcelRes(ResCodeData cd)
        //{
        //    try
        //    {


        //        //int countTemp = 0;

        //        //do
        //        //{
        //        //    countTemp++;
        //        //} while (((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != "" && ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != null);
        //        ////count = countTemp;
        //        //xSheet.Cells[countTemp, 1] = cd.resCodeStr;
        //        //xSheet.Cells[countTemp, 2] = cd.currentRes + "Ω";
        //        //if (cd.isQualified)
        //        //    xSheet.Cells[countTemp, 3] = "合格";
        //        //else
        //        //    xSheet.Cells[countTemp, 3] = "不合格";

        //        ////if (cd.checkMode==0)
        //        //xSheet.Cells[countTemp, 4] = "电阻检测";


        //        //xSheet.Cells[countTemp, 5] = cd.checkTime;

        //        //count++;


        //    }
        //    catch
        //    { }

        //}

        //public void CloseExcel()
        //{
        //    try
        //    {
        //        xBook.Save();
        //        xBook.Close(Missing.Value, Missing.Value, Missing.Value);
        //        xApp.Quit();
        //    }
        //    catch
        //    { }

        //}

       
        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否复检？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                try
                {
                    checkAgainMode = 4;
                    ecd = new EleCodeData();
                    //rcd = new ResCodeData();
                    //rcd.isStartReceive = true;
                    //发送电阻检测
                    //DianZuTest();
                    Thread thTemp = new Thread(new ThreadStart(LeftCheckThMethod));
                    //thRightCheck = new Thread(new ThreadStart(RightCheckThMethod));
                    thTemp.IsBackground = true;
                    thTemp.Start();


                    //crc.LeftCheckTime = DateTime.Now;
                }
                catch
                { }
            }
            //try
            //{
            //    if (dianzuPort.IsOpen)
            //    {
            //        byte[] b = { 0xAB, 0xAD, 0xAF };
            //        dianzuPort.Write(b, 0, b.Length);
            //    }
            //}
            //catch
            //{ }                     
        }

        private void DianZuTest()
        {
            try
            {
                if (dianzuPort.IsOpen)
                {
                    byte[] b = { 0xAB, 0x9D, 0xAF };
                    dianzuPort.Write(b, 0, b.Length);
                }
            }
            catch
            { }                   
        }

        private void setBt_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            panel3.Visible = false;

            tabControl1.SelectedTabIndex = 0;
            toolStripButton1.Visible = true;
            toolStripButton2.Visible = false;

            //UpdateCheckDianliu(1);

            //crc = new CheckResultClass();
            //crc.SeatName = "Test";
            //crc.PartNum = "Test";
            //crc.TotalCode = "Test";
            //crc.LeftCode = "Test";
            //crc.RightCode = "Test";
            //crc.LeftDianZuValue = 2;
            //crc.LeftDianLiuValue = 2;
            //crc.LeftCheckResult = "Test";
            //crc.LeftCheckTime = DateTime.Now;
            //crc.LeftFinishTime = DateTime.Now;
            //crc.RightDianZuValue = 2;
            //crc.RightDianLiuValue = 2;
            //crc.RightCheckResult = "Test";
            //crc.RightCheckTime = DateTime.Now;
            //crc.RightFinishTime = DateTime.Now;
            //checkResultList.Add(crc);


            dgResult.Rows.Clear();

            checkResList = checkResBll.GetModelList("");
            for (int i = 0; i < checkResList.Count; i++)
            {
                int index = dgResult.Rows.Add();

                dgResult.Rows[index].Cells[0].Value = checkResList[i].vehicle_name;
                dgResult.Rows[index].Cells[1].Value = checkResList[i].part_id;
                dgResult.Rows[index].Cells[2].Value = checkResList[i].sbr1_value + "Ω";

                dgResult.Rows[index].Cells[3].Value = checkResList[i].sbr1_check_result;
                dgResult.Rows[index].Cells[4].Value = checkResList[i].sbr2_value + "Ω";
                dgResult.Rows[index].Cells[5].Value = checkResList[i].sbr2_check_result;
                dgResult.Rows[index].Cells[6].Value = checkResList[i].sbr3_value + "Ω";

                dgResult.Rows[index].Cells[7].Value = checkResList[i].sbr3_check_result;
                dgResult.Rows[index].Cells[8].Value = checkResList[i].left_dianliu_value + "A";
                dgResult.Rows[index].Cells[9].Value = checkResList[i].left_check_result;
                dgResult.Rows[index].Cells[10].Value = checkResList[i].right_dianliu_value + "A";
                dgResult.Rows[index].Cells[11].Value = checkResList[i].right_check_result;
            }
        }

        private void autoBt_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = false;
            panel3.Visible = true;

            toolStripButton1.Visible = false;
            toolStripButton2.Visible = false;

            //if (AutoModel == 0)
            //{
            //    button1.Text = "切换手动";
            //}
            //else
            //    button1.Text = "切换自动";

            //UpdateCheck();
            //StartTest();
            //  ((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");
            //((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");
            //((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");
            //((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");
            //((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");
            //((DataGridViewComboBoxColumn)dgLingjian.Columns[0]).Items.Add("1");

            //dgLingjian.Rows.Clear();
            //GetCodeData();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTabIndex == 0)
            {
                try
                {
                    for (int i = 0; i < dgLingjian.Rows.Count - 1; i++)
                    {
                        if (checkProList.Count > 0)
                        {
                            if (!checkProBll.Exists(dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString()))  // !checkProList.ContainsKey(dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString()))
                            {
                                Model.Check_Product cp = new Model.Check_Product();

                                cp.vehicle_type = dgLingjian.Rows[i].Cells[0].EditedFormattedValue.ToString();
                                cp.seat_loc = dgLingjian.Rows[i].Cells[1].EditedFormattedValue.ToString();
                                cp.part_id = dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString();
                                cp.left_code = dgLingjian.Rows[i].Cells[3].EditedFormattedValue.ToString();
                                cp.right_code = dgLingjian.Rows[i].Cells[4].EditedFormattedValue.ToString();
                                cp.sbr1_code = dgLingjian.Rows[i].Cells[5].EditedFormattedValue.ToString();
                                cp.sbr2_code = dgLingjian.Rows[i].Cells[6].EditedFormattedValue.ToString();
                                cp.sbr3_code = dgLingjian.Rows[i].Cells[7].EditedFormattedValue.ToString();
                                cp.is_check_dianliu = Convert.ToBoolean(dgLingjian.Rows[i].Cells[8].EditedFormattedValue.ToString());
                                cp.is_check_dianzu = Convert.ToBoolean(dgLingjian.Rows[i].Cells[9].EditedFormattedValue.ToString());

                                checkProBll.Add(cp);

                                //WriteCodeData(dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString(), codeTemp);
                            }
                            //else
                            //{
                            //    MessageBox.Show("存在重复零件号");

                            //    return;
                            //}

                        }
                        else
                        {
                            Model.Check_Product cp = new Model.Check_Product();

                            cp.vehicle_type = dgLingjian.Rows[i].Cells[0].EditedFormattedValue.ToString();
                            cp.seat_loc = dgLingjian.Rows[i].Cells[1].EditedFormattedValue.ToString();
                            cp.part_id = dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString();
                            cp.left_code = dgLingjian.Rows[i].Cells[3].EditedFormattedValue.ToString();
                            cp.right_code = dgLingjian.Rows[i].Cells[4].EditedFormattedValue.ToString();
                            cp.sbr1_code = dgLingjian.Rows[i].Cells[5].EditedFormattedValue.ToString();
                            cp.sbr2_code = dgLingjian.Rows[i].Cells[6].EditedFormattedValue.ToString();
                            cp.sbr3_code = dgLingjian.Rows[i].Cells[7].EditedFormattedValue.ToString();
                            cp.is_check_dianliu = Convert.ToBoolean(dgLingjian.Rows[i].Cells[8].EditedFormattedValue.ToString());
                            cp.is_check_dianzu = Convert.ToBoolean(dgLingjian.Rows[i].Cells[9].EditedFormattedValue.ToString());

                            checkProBll.Add(cp);

                            //WriteCodeData(dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString(), codeTemp);
                        }
                    }

                    dgLingjian.Rows.Clear();
                    GetCodeDataDataBase();

                    MessageBox.Show("保存成功");
                }
                catch
                {
                    MessageBox.Show("保存异常");
                }
               
            }
            else if (tabControl1.SelectedTabIndex == 1)
            {
                try
                {
                    for (int i = 0; i < dgRule.Rows.Count - 1; i++)
                    {
                        if (codeRuleList.Count > 0)
                        {
                            if (!codeRuleBll.Exists(dgRule.Rows[i].Cells[0].EditedFormattedValue.ToString()))  // !checkProList.ContainsKey(dgLingjian.Rows[i].Cells[2].EditedFormattedValue.ToString()))
                            {
                                Model.Code_Rule cr = new Model.Code_Rule();

                                cr.rule_name = dgRule.Rows[i].Cells[0].EditedFormattedValue.ToString();
                                cr.rule_detail = dgRule.Rows[i].Cells[1].EditedFormattedValue.ToString();
                                cr.rule_describe = dgRule.Rows[i].Cells[2].EditedFormattedValue.ToString();
                               

                                codeRuleBll.Add(cr);

                            }
                        
                        }
                        else
                        {
                            Model.Code_Rule cr = new Model.Code_Rule();

                            cr.rule_name = dgRule.Rows[i].Cells[0].EditedFormattedValue.ToString();
                            cr.rule_detail = dgRule.Rows[i].Cells[1].EditedFormattedValue.ToString();
                            cr.rule_describe = dgRule.Rows[i].Cells[2].EditedFormattedValue.ToString();


                            codeRuleBll.Add(cr);

                        }
                    }

                    dgRule.Rows.Clear();
                    GetCodeRuleDataBase();

                    MessageBox.Show("保存成功");
                }
                catch
                {
                    MessageBox.Show("保存异常");
                }
            }
            else if (tabControl1.SelectedTabIndex == 2)
            {
                try
                {
                    dianZuDown = Convert.ToDouble(dgCheckPara.Rows[0].Cells[1].EditedFormattedValue.ToString());
                    dianZuUp = Convert.ToDouble(dgCheckPara.Rows[0].Cells[2].EditedFormattedValue.ToString());
                    //dianLiuHighDown = Convert.ToDouble(dgCheckPara.Rows[1].Cells[1].EditedFormattedValue.ToString());
                    //dianLiuHighUp = Convert.ToDouble(dgCheckPara.Rows[1].Cells[2].EditedFormattedValue.ToString());
                    dianLiuMidDown = Convert.ToDouble(dgCheckPara.Rows[1].Cells[1].EditedFormattedValue.ToString());
                    dianLiuMidUp = Convert.ToDouble(dgCheckPara.Rows[1].Cells[2].EditedFormattedValue.ToString());
                    //dianLiuLowDown = Convert.ToDouble(dgCheckPara.Rows[3].Cells[1].EditedFormattedValue.ToString());
                    //dianLiuLowUp = Convert.ToDouble(dgCheckPara.Rows[3].Cells[2].EditedFormattedValue.ToString());

                    dgCheckPara.Rows.Clear();
                    IniCheckComData();

                    WriteCheckData("resistance", dianZuDown + "-" + dianZuUp);
                    //WriteCheckData("currentHigh", dianLiuHighDown + "-" + dianLiuHighUp);
                    WriteCheckData("currentMid", dianLiuMidDown + "-" + dianLiuMidUp);
                    //WriteCheckData("currentLow", dianLiuLowDown + "-" + dianLiuLowUp);

                    MessageBox.Show("保存成功");
                }
                catch
                { MessageBox.Show("保存异常"); }


            }

            
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label11.Text=DateTime.Now.ToString();
        }

        private void tabItem6_Click(object sender, EventArgs e)
        {
            toolStripButton2.Visible = true;

            toolStripButton1.Visible = false;
        }

        private void tabItem1_Click(object sender, EventArgs e)
        {
            toolStripButton1.Visible = true;

            toolStripButton2.Visible = false;
        }

        private void tabItem2_Click(object sender, EventArgs e)
        {
            toolStripButton1.Visible = true;

            toolStripButton2.Visible = false;
        }


        List<CheckExport> agvInfoExports = new List<CheckExport>();
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < checkResList .Count; i++)
                {

                    CheckExport ce = new CheckExport();
                    ce.VehicleName = checkResList[i].vehicle_name;
                    ce.SeatName = checkResList[i].seat_loc;
                    ce.PartNum = checkResList[i].part_id;
                    ce.Sbr1Code = checkResList[i].sbr1_code;
                    ce.Sbr1Value = checkResList[i].sbr1_value + "Ω";
                    ce.Sbr1Result = checkResList[i].sbr1_check_result;
                    ce.Sbr2Code = checkResList[i].sbr2_code;
                    ce.Sbr2Value = checkResList[i].sbr2_value + "Ω";
                    ce.Sbr2Result = checkResList[i].sbr2_check_result;
                    ce.Sbr3Code = checkResList[i].sbr3_code;
                    ce.Sbr3Value = checkResList[i].sbr3_value + "Ω";
                    ce.Sbr3Result = checkResList[i].sbr3_check_result;

                    ce.LeftCode = checkResList[i].left_code;
                    ce.LeftDl = checkResList[i].left_dianliu_value + "A";
                    ce.LeftRes = checkResList[i].left_check_result;
                    try
                    { ce.Lstime = (DateTime)checkResList[i].left_check_starttime; }
                    catch
                    { ce.Lstime = DateTime.Now; }
                    
                    try
                    { ce.Lftime = (DateTime)checkResList[i].left_check_endtime; }
                    catch
                    { ce.Lftime = DateTime.Now; }
                    

                    ce.RightCode = checkResList[i].right_code;
                    ce.RightDl = checkResList[i].right_dianliu_value + "A";
                    ce.RightRes = checkResList[i].right_check_result;
                    try
                    { ce.Rstime = (DateTime)checkResList[i].right_check_starttime; }
                    catch
                    { ce.Rstime = DateTime.Now; }
                   
                    try
                    { ce.Rftime = (DateTime)checkResList[i].right_check_endtime; }
                    catch
                    { ce.Rftime = DateTime.Now; }
                    
                    agvInfoExports.Add(ce);
                    //agvInfoExports.Add(new CheckExport()
                    //        {
                    //            VehicleName=checkResList[i].vehicle_name,
                    //            SeatName = checkResList[i].seat_loc,
                    //            PartNum = checkResList[i].part_id,

                    //            Sbr1Code = checkResList[i].sbr1_code,
                    //            Sbr1Value = checkResList[i].sbr1_value + "Ω",
                    //            Sbr1Result = checkResList[i].sbr1_check_result,

                    //            Sbr2Code = checkResList[i].sbr2_code,
                    //            Sbr2Value = checkResList[i].sbr2_value + "Ω",
                    //            Sbr2Result = checkResList[i].sbr2_check_result,

                    //            Sbr3Code = checkResList[i].sbr3_code,
                    //            Sbr3Value = checkResList[i].sbr3_value + "Ω",
                    //            Sbr3Result = checkResList[i].sbr3_check_result,

                    //            LeftCode = checkResList[i].left_code,
                    //            LeftDl = checkResList[i].left_dianliu_value + "A",

                    //            LeftRes = checkResList[i].left_check_result,
                               
                    //            Lstime = (DateTime)checkResList[i].left_check_starttime,
                    //            Lftime = (DateTime)checkResList[i].left_check_endtime,

                    //            RightCode = checkResList[i].right_code,
                    //            RightDl = checkResList[i].right_dianliu_value + "A",

                    //            RightRes = checkResList[i].right_check_result,
                    //            Rstime = (DateTime)checkResList[i].right_check_starttime,
                    //            Rftime = (DateTime)checkResList[i].right_check_endtime,

                    //        });
                }
            }
            catch
            { }


            try
            {
                DialogResult dr = MessageBox.Show("是否当前数据生成EXCEL报表？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr == DialogResult.OK)
                {
                    if (checkResList.Count != 0)
                    {
                        SaveFileDialog dialog = new SaveFileDialog();
                        dialog.Filter = "Excel表格(*.xls)|*.xls";
                        if (dialog.ShowDialog() == DialogResult.OK)
                        {

                            NpoiExport<CheckExport>.SaveXlsChangeds(dialog.FileName, agvInfoExports);

                            MessageBox.Show("导出完成");
                        }
                    }
                    else
                    {
                        MessageBox.Show("无数据可导出！！！");
                    }
                }
            }
            catch
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //if (AutoModel == 0)
            //{
                DialogResult dr = MessageBox.Show("是否复检？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr == DialogResult.OK)
                {
                    try
                    {
                        checkAgainMode = 1;

                        rcd = new ResCodeData();
                        rcd.isStartReceive = true;
                        //发送电阻检测

                        DianZuTest();
                    }
                    catch
                    { }
                }
           
            //else
            //{
            //    DialogResult dr = MessageBox.Show("是否切换自动模式？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            //    if (dr == DialogResult.OK)
            //    {
            //        AutoModel = 0;
            //        button1.Text = "切换手动";

            //        button1.ForeColor = Color.Lime;
            //    }
            //}

            
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("是否复位当前检测？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr == DialogResult.OK)
                {
                    scanControl = 0;
                    checkAgainMode = 0;

                    label12.Text = "--";
                    label15.Text = "--";
                    label19.Text = "--";
                    label13.Text = "--";
                    label25.Text = "--";
                    label22.Text = "--";
                    label21.Text = "--";
                    label17.Text = "--";
                    label16.Text = "--";
                    label18.Text = "--";
                    label14.Text = "--";
                    label20.Text = "--";


                    label15.ForeColor = Color.Lime;
                    label19.ForeColor = Color.Lime;
                    label25.ForeColor = Color.Lime;
                    label22.ForeColor = Color.Lime;
                    label21.ForeColor = Color.Lime;
                    label17.ForeColor = Color.Lime;
                    label16.ForeColor = Color.Lime;
                    label18.ForeColor = Color.Lime;
                    label14.ForeColor = Color.Lime;
                    label20.ForeColor = Color.Lime;

                    label3.ForeColor = Color.Lime;
                    label26.ForeColor = Color.Lime;
                    label4.ForeColor = Color.Lime;
                    label24.ForeColor = Color.Lime;
                    label23.ForeColor = Color.Lime;

                    label2.ForeColor = Color.Lime;
                    label12.ForeColor = Color.Lime;
                    label13.ForeColor = Color.Lime;


                    dgCheck.Rows.Clear();

                    sbr1CodeControl = true;
                    sbr2CodeControl = true;
                    sbr3CodeControl = true;

                    leftCodeControl = true;
                    rightCodeControl = true;

                    sbr1Ng = false;
                    sbr2Ng = false;
                    sbr3Ng = false;
                    leftNg = false;
                    rightNg = false;


                    //label5.Text = "";
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button3.Enabled = false;
                    button4.Enabled = false;
                    button5.Enabled = false;

                    
                }
            }
            catch
            {

            }
        }

        int selectIndex = 100;
        private void dgLingjian_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex > -1)
                    {


                        selectIndex = e.RowIndex;

                        contextMenuStrip1.Show(Cursor.Position);
                        
                        this.dgLingjian.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Lime;

                        for (int i = 0; i < dgLingjian.Rows.Count; i++)
                        {
                            if (i != e.RowIndex)
                                this.dgLingjian.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                        }


                    }
                }
            }
            catch
            { }
        }


        UpdateForm uf;
        private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                if (checkProBll.Exists(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString()))
                {
                    uf = new UpdateForm(tempDianLiuMid,dgLingjian.Rows[selectIndex]);
                    uf.UpdateEvent += UpdateMethod;
                    uf.ShowDialog();
                }
               
                //dgLingjian.Rows[selectIndex].ReadOnly = false;
                //if (checkProBll.Exists(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString()))
                //{
                //    checkProBll.Delete(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString());
                //    dgLingjian.Rows.Clear();
                //    GetCodeDataDataBase();

                //    MessageBox.Show("删除成功");
                //}

            }
            catch
            { MessageBox.Show("删除异常"); }
            //string s = this.dgLingjian.Rows[index].Cells[1].Value.ToString();
            //string s1 = this.dgLingjian.Rows[index].Cells[2].Value.ToString();
            //string s2 = this.dgLingjian.Rows[index].Cells[3].Value.ToString();
            //string s3 = this.dgLingjian.Rows[index].Cells[4].Value.ToString();
            //string s4 = this.dgLingjian.Rows[index].Cells[5].Value.ToString();
        }


        private void UpdateMethod(string[] str)
        {
            
            for (int i = 0; i < checkProList.Count; i++)
            {
                if (checkProList[i].part_id == dgLingjian.Rows[selectIndex].Cells[2].Value.ToString())
                {
                    if (str[2] == dgLingjian.Rows[selectIndex].Cells[2].Value.ToString())
                    {
                        checkProList[i].vehicle_type = str[0];
                        checkProList[i].seat_loc = str[1];
                        checkProList[i].left_code = str[3];
                        checkProList[i].right_code = str[4];
                        checkProList[i].sbr1_code = str[5];
                        checkProList[i].sbr2_code = str[6];
                        checkProList[i].sbr3_code = str[7];
                        checkProList[i].is_check_dianliu = Convert.ToBoolean(str[8]);
                        checkProList[i].is_check_dianzu = Convert.ToBoolean(str[9]);

                        checkProBll.Update(checkProList[i]);
                        MessageBox.Show("更新成功");
                        uf.Close();
                        break;
                    }
                    else
                    {
                        if (!checkProBll.Exists(str[2]))
                        {
                            checkProBll.Delete(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString());


                            Model.Check_Product cp = new Model.Check_Product();

                            cp.vehicle_type = str[0];
                            cp.seat_loc = str[1];
                            cp.part_id = str[2];
                            cp.left_code = str[3];
                            cp.right_code = str[4];
                            cp.sbr1_code = str[5];
                            cp.sbr2_code = str[6];
                            cp.sbr3_code = str[7];
                            cp.is_check_dianliu = Convert.ToBoolean(str[8]);
                            cp.is_check_dianzu = Convert.ToBoolean(str[9]);

                            checkProBll.Add(cp);

                            MessageBox.Show("更新成功");
                            uf.Close();
                           
                            break;
                        }
                        else
                        {
                            MessageBox.Show("存在重复零件号");

                            return;
                        }
                    }
                }
            }

            dgLingjian.Rows.Clear();
            GetCodeDataDataBase();
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkProBll.Exists(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString()))
                {
                    checkProBll.Delete(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString());
                    dgLingjian.Rows.Clear();
                    GetCodeDataDataBase();

                    MessageBox.Show("删除成功");
                }
                   
            }
            catch
            { MessageBox.Show("删除异常"); }
        }

        private void tabItem11_Click(object sender, EventArgs e)
        {
            toolStripButton1.Visible = true;

            toolStripButton2.Visible = false;
        }

        private void tabControl1_TabMoved(object sender, DevComponents.DotNetBar.TabStripTabMovedEventArgs e)
        {
            e.Cancel = true;
        }


        int codeIndex = 100;
        private void dgRule_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex > -1)
                    {


                        codeIndex = e.RowIndex;

                        contextMenuStrip2.Show(Cursor.Position);

                        dgRule.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Lime;

                        for (int i = 0; i < dgRule.Rows.Count; i++)
                        {
                            if (i != e.RowIndex)
                                dgRule.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                        }


                    }
                }
            }
            catch
            { }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            try
            {
                if (codeRuleBll.Exists(dgRule.Rows[codeIndex].Cells[0].Value.ToString()))
                {
                    codeRuleBll.Delete(dgRule.Rows[codeIndex].Cells[0].Value.ToString());
                    dgRule.Rows.Clear();
                    GetCodeRuleDataBase();

                    MessageBox.Show("删除成功");
                }

            }
            catch
            { MessageBox.Show("删除异常"); }
        }

    }


    public class CheckExport
    {
        [Description("设备名称")]
        public string VehicleName { get; set; }
        [Description("座椅")]
        public string SeatName { get; set; }
        [Description("零件号")]
        public string PartNum { get; set; }

        [Description("SBR1电阻条码")]
        public string Sbr1Code { get; set; }
        [Description("SBR1电阻值")]
        public string Sbr1Value { get; set; }
        [Description("SBR1电阻测试结果")]
        public string Sbr1Result { get; set; }

        [Description("SBR2电阻条码")]
        public string Sbr2Code { get; set; }
        [Description("SBR2电阻值")]
        public string Sbr2Value { get; set; }
        [Description("SBR2电阻测试结果")]
        public string Sbr2Result { get; set; }

        [Description("SBR3电阻条码")]
        public string Sbr3Code { get; set; }
        [Description("SBR3电阻值")]
        public string Sbr3Value { get; set; }
        [Description("SBR3电阻测试结果")]
        public string Sbr3Result { get; set; }
      

        [Description("左椅条码")]
        public string LeftCode { get; set; }
        
        [Description("左椅电流值")]
        public string LeftDl { get; set; }
        [Description("左椅测试结果")]
        public string LeftRes { get; set; }
        [Description("左椅测试开始时间")]
        public DateTime Lstime { get; set; }
        [Description("左椅测试完成时间")]
        public DateTime Lftime { get; set; }

        [Description("右椅条码")]
        public string RightCode { get; set; }
      
        [Description("右椅电流值")]
        public string RightDl { get; set; }
        [Description("右椅测试结果")]
        public string RightRes { get; set; }
        [Description("右椅测试开始时间")]
        public DateTime Rstime { get; set; }
        [Description("右椅测试完成时间")]
        public DateTime Rftime { get; set; }

        //[Description("停留时间(s)")]
        //public string StayTime { get; set; }
        //[Description("停留原因")]
        //public string StayReason { get; set; }
    }

    public class EleCodeData
    {
        //public string eleCodeStr;

        public double currentEle;

        public DateTime checkTime;

        //public bool isQualified;

        public bool isOut;

        public EleCodeData()
        {
            //eleCodeStr = string.Empty;
            currentEle = 0;
            //isQualified = false;
            isOut = false;
        }
    }


    public class ResCodeData
    {
        //public string resCodeStr;

        public double currentRes;

        public DateTime checkTime;

        //public bool isQualified;

        public bool isStartReceive;

        public ResCodeData()
        {
           // resCodeStr = string.Empty;
            currentRes = 0;
            //isQualified = false;
            isStartReceive = false;
        }
    }


    public class CodeClass
    {
        public string VehicleName = string.Empty;

        public string SeatName=string.Empty;

        public string SeatLoc = string.Empty;

        public string PartNum = string.Empty;

        //public string TotalCode = string.Empty;

        public string LeftCode = string.Empty;

        public string RightCode = string.Empty;

        public string Sbr1Code = string.Empty;

        public string Sbr2Code = string.Empty;

        public string Sbr3Code = string.Empty;

        public bool IsCheckDianLiu;

        public bool IsCheckDianZu;

      
    }


    public class CheckCompareClass
    {
        public int CheckType;

        public double UpValue;

        public double DownValue;
    }


    public class CheckResultClass
    {
        public string VehicleName = string.Empty;
        public string SeatName = string.Empty;
        public string PartNum = string.Empty;

        //public string TotalCode = string.Empty;
        public string Sbr1Code = string.Empty;
        public string Sbr2Code = string.Empty;
        public string Sbr3Code = string.Empty;

        public double Sbr1DianZuValue = 0;
        public string Sbr1CheckResult = "--";
        public double Sbr2DianZuValue = 0;
        public string Sbr2CheckResult = "--";
        public double Sbr3DianZuValue = 0;
        public string Sbr3CheckResult = "--";
        public string LeftCode = string.Empty;

        //public double LeftDianZuValue = 0;

        public double LeftDianLiuValue = 0;

        public string LeftCheckResult = string.Empty;

        public DateTime LeftCheckTime;

        public DateTime LeftFinishTime;

        public string RightCode = string.Empty;

       // public double RightDianZuValue = 0;

        public double RightDianLiuValue = 0;

        public string RightCheckResult = string.Empty;

        public DateTime RightCheckTime;

        public DateTime RightFinishTime;


    }
}
